package cn.zysheep;



import cn.zysheep.domain.Person;
import cn.zysheep.domain.PersonVO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.Date;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/10
 */
public class BeanUtilsTest {

    /**
     * void copyProperties(Object source, Object target)
     * 将给定source源 bean 的属性值复制到target目标 bean中
     *
     */
    @Test
    public void testCopyProperties() throws IOException {
        Person person = new Person(1,"小明同学",new Date(),3000F);
        PersonVO personVO = new PersonVO();
        BeanUtils.copyProperties(person,personVO);
        //PersonVO{name='小明同学', birthday=Tue Aug 10 16:37:27 CST 2021}
        System.out.println(personVO);
    }

    /**
     *void copyProperties(Object source, Object target)
     * 将给定source源 bean 的属性值复制到target目标 bean中
     */
    @Test
    public void testCopyProperties2() throws IOException {
        Person person = new Person(1,"小明同学",new Date(),3000F);
        Person person1 = new Person();
        BeanUtils.copyProperties(person,person1);
        System.out.println(person1);
    }

    /**
     * void copyProperties(Object source, Object target, String... ignoreProperties)
     * 将给定source源 bean 的属性值复制到target目标 bean中
     *  String... ignoreProperties 要忽略的属性名称数组
     */
    @Test
    public void testCopyProperties3() throws IOException {
        Person person = new Person(1,"小明同学",new Date(),3000F);
        Person person1 = new Person();
        // Person{id=1, name='null', birthday=Tue Aug 10 16:36:41 CST 2021, salary=3000.0}
        BeanUtils.copyProperties(person,person1,"name");
        System.out.println(person1);
    }

    /**
     *T instantiateClass(Class<T> clazz)
     * 使用其“主”构造函数或其默认构造函数实例化一个类
     */
    @Test
    public void testInstantiateClass() {
        BeanUtilsTest beanUtilsTest = BeanUtils.instantiateClass(BeanUtilsTest.class);

        System.out.println(beanUtilsTest);
    }

}
